package com.yjweb.webportal.entities;

import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * Created by windMan on 2018/5/30
 * 分页辅助类
 */

public class Pagination<T> implements Serializable {
    private List<T> resultList;
    private PageInfo pageInfo;

    public Pagination(){

    }

    public Pagination(List<T> list){
        if (list instanceof Page) {
            Page page = (Page) list;
            pageInfo=new PageInfo();
            pageInfo.setPageIndex(page.getPageNum());
            pageInfo.setPageSize(page.getPageSize());
            pageInfo.setPageCount(page.getPages());
            pageInfo.setTotalCount(page.getTotal());
            resultList=page;
        } else if (list instanceof Collection) {
            pageInfo=new PageInfo();
            pageInfo.setPageIndex(1);
            pageInfo.setPageSize(list.size());
            pageInfo.setTotalCount(list.size());
            pageInfo.setPageCount(1);
            resultList=list;
        }
    }

    public PageInfo getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo pageInfo) {
        this.pageInfo = pageInfo;
    }

    public List<T> getResultList() {
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    public static Pagination empty(){
        Pagination pagination=new Pagination();
        PageInfo pageInfo=new PageInfo();
        pageInfo.setPageCount(0);
        pageInfo.setPageIndex(0);
        pageInfo.setPageSize(0);
        pageInfo.setTotalCount(0);
        pagination.setPageInfo(pageInfo);
        return pagination;
    }
}